﻿using System;
using System.Linq;
using System.Text;
using System.Data;


namespace KTX.Data
{
   public partial class QLKTXEntities
   {
        public override int SaveChanges(System.Data.Objects.SaveOptions options)
        {
            foreach (System.Data.Objects.ObjectStateEntry entry in
            ObjectStateManager.GetObjectStateEntries(
            EntityState.Added | EntityState.Modified))
            {
                var error = entry.Entity.GetType()
                     .GetProperties().Any() ?
                     (entry.Entity.GetType()
                     .GetProperties().ToList<System.Reflection.PropertyInfo>().Where(p => p.Name == "Error").Any() ?
                    entry.Entity.GetType()
                    .GetProperties().ToList<System.Reflection.PropertyInfo>().Where(p => p.Name == "Error").First()
                    .GetValue(entry.Entity, null).ToString() : string.Empty) : string.Empty;

                if (error != string.Empty)
                {
                    throw new ArgumentException(error);
                }

            }

            return base.SaveChanges(options);
        }
    }
}
